<?php

/**
 * This is the model class for table "{{mem_sub}}".
 *
 * The followings are the available columns in table '{{mem_sub}}':
 * @property integer $zms_id
 * @property string $zms_username
 * @property string $zms_password
 * @property string $zms_name
 * @property integer $zms_sex
 * @property integer $zpc_id_dep
 * @property integer $zpc_id_role
 * @property string $zms_code
 * @property integer $zm_id
 * @property integer $inputtime
 */
class MemSub extends CActiveRecord
{
	//重复密码
	public $re_password;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemSub the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_sub}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zms_username, zms_password,zms_name,zpc_id_dep', 'required'),
			array('zms_username', 'unique','caseSensitive'=>false,'className'=>'MemSub','message'=>'用户名"{value}"已经被注册，请更换'),
			array('zms_id, zms_sex, zpc_id_dep, zpc_id_role, zm_id, inputtime', 'numerical', 'integerOnly'=>true),
			array('re_password', 'compare', 'compareAttribute'=>'zms_password','on'=>'register','message'=>'两处输入的密码并不一致'),
			array('zms_username, zms_name,zpc_duties', 'length', 'max'=>20),
			array('zms_code', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zms_id, zms_username, zms_password, zms_name, zms_sex, zpc_id_dep, zpc_id_role, zms_code, zm_id, zpc_duties, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zms_id' => 'ID',
			'zms_username' => '用户名：',
			'zms_password' => '密码：',
			're_password' => '确认密码：',
			'zms_name' => '姓名：',
			'zms_sex' => '姓别：',
			'zpc_id_dep' => '部门：',
			'zpc_id_role' => '角色：',
			'zpc_duties' => '职务：',
			'zms_code' => '识别码',
			'zm_id' => '公司ID',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zms_id',$this->zms_id);
		$criteria->compare('zms_username',$this->zms_username,true);
		$criteria->compare('zms_password',$this->zms_password,true);
		$criteria->compare('zms_name',$this->zms_name,true);
		$criteria->compare('zms_sex',$this->zms_sex);
		$criteria->compare('zpc_id_dep',$this->zpc_id_dep);
		$criteria->compare('zpc_id_role',$this->zpc_id_role);
		$criteria->compare('zms_code',$this->zms_code,true);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/*
	 * 获取部门员工，
	 * @param $sectionid 部门编辑-关联自定义分类表
	 * */
	public function getAccountStaff($sectionid){
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$criteria->compare('zpc_id_dep',$sectionid);
		$staff = $this->findAll($criteria);
		return $staff;
	}
	
	/*
	 * 获取姓别
	 * */
	public function getSex($type=null){
		if($type==null){
			return array(
				'1'=>'男',
				'2'=>'女',		
			);
		}else{
			$level = $this->getSex();
			if(array_key_exists($type,$level)){
				return $level[$type];
			}
		}
	}
	
	/*
	 * 保存数据前的操作
	 * */
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
				//获取密码识别码
				$this->zms_code = Adminer::model()->createCode();
				//获取公司ID
				$this->zm_id = Company::model()->getCmpUserId();
				$this->re_password = $this->passwordEncode($this->re_password);
				$this->zms_password = $this->passwordEncode($this->zms_password);
			}else{
			//	$this->re_password = $this->passwordEncode($this->re_password);
			//	$this->zms_password = $this->passwordEncode($this->zms_password);
			}
			return true;
		}else{
			return false;
		}
	}
	
	//加密
	public function passwordEncode($password){
		return md5( md5( $password ).$this->zms_code);
	}
	
	//验证密码
	public function checkPassword($pass){
		if($this->passwordEncode($pass)==$this->zms_password){
			//echo 1;exit;
			return true;
		}else{
			echo $this->passwordEncode($pass)."<br>".$this->zms_password;
			echo 2;exit;
			return false;
		}
	}
	
	/*
	*@持久化子帐号信息，登录时用,调用信息方法：Yii::app()->user->username;
	*@param $username 子帐号用户名
	*根据需要适当增加
	*/
	public function setUserInfo($username='')
	{
		if(Yii::app()->user->isGuest){
			throw new Exception('用户未登录！');
		}
		if($this->isNewRecord){
			$username = $username? $username : $this->zms_username;			
			$record = MemSub::model()->findByAttributes(array('zms_name'=>$username));
		}else{
			$record = &$this;
		}
		//print_r($record);exit;
		Yii::app()->user->setState('userid',$record->zms_id,1);
		Yii::app()->user->setState('username',$record->zms_username,1);
		Yii::app()->user->setState('name',$record->zms_name,1);
		Yii::app()->user->setState('department',$record->zpc_id_dep,1);//部门
		Yii::app()->user->setState('duty',$record->zpc_duties,1);
		Yii::app()->user->setState('role',$record->zpc_id_role,1);
		Yii::app()->user->setState('parent_id',$record->zm_id,1);
	}
	
}